﻿// Shader-Based-Image-Processing (SBIP)
// http://code.google.com/p/sbip/
//
// Copyright © Frank Nagl, 2009-2010
// admin@franknagl.de
//
using System;

namespace SBIP.Filter
{
    /// <summary>
    /// Posterization of an image.
    /// </summary>
    /// <remarks><para>Posterization of an image entails conversion of a 
    /// continuous gradation of tone to several regions of fewer tones, 
    /// with abrupt changes from one tone to another.</para>
    /// <para>Sample usage:</para>
    /// <code>
    /// Bitmap image = new Bitmap("cathedral.jpg");
    /// 
    /// // 1. Offline rendering of images
    /// // create Processor, used as rendering framework
    /// Processor processor = new Processor();
    /// // starts Processor
    /// processor.Begin( image );
    /// // create Posterization filter
    /// Posterization filter = new Posterization( );
    /// processor.Filter = filter;
    /// // optional: configure filter
    /// ...
    /// // apply the filter, render to bitmap  
    /// Bitmap resultImage = processor.RenderToBitmap( );
    /// resultImage.Save( "Posterization.jpg", ImageFormat.Jpeg );
    /// // apply the filter, render to texture
    /// // Texture2D resultTexture = processor.RenderToTexture( );
    /// processor.End( );
    ///  
    /// // 2. Online rendering of videos or images
    /// // create any windows control for rendering in
    /// Form myForm = new Form
    /// {
    ///     Width = 1280,
    ///     Height = 720
    /// };
    /// // starts Processor
    /// processor.Begin( "Video.avi", myForm );
    /// // create Posterization filter
    /// filter = new Posterization( );
    /// processor.Filter = filter;
    /// // optional: configure filter
    /// ...
    /// // apply the filter
    /// myForm.Show( );
    /// while ( myForm.Created )
    /// {
    ///     processor.Render( );
    ///     Application.DoEvents( );
    /// }
    /// processor.End( );
    /// </code>
    /// 
    /// <para><b>Initial image:</b></para>
    /// <img src="../../Cathedral.jpg" width="500" height="338" />
    /// <para><b>Result image:</b></para>
    /// <img src="../../Posterization.png" width="500" height="338" />
    /// </remarks>
    /// <seealso cref="Filter.Posterization"/>
    public sealed class Posterization : BaseFilter
    {
        #region effect properties
        private byte threshold;
        /// <summary>
        /// Posterization threshold. Default: 90.
        /// </summary>
        public byte Threshold
        {
            get { return threshold; }
            set
            {
                if (Effect == null)
                    throw new ArgumentException("Threshold" + InitMsg);

                threshold = value;
                Effect.Parameters["Threshold"].SetValue(value);
            }
        }
        #endregion effect properties

        /// <summary>
        /// Initializes a new instance of the <see cref="Invert"/> class.
        /// </summary>
        public Posterization() : base("Posterization") { }

        /// <summary>
        /// Defines posterization algorithm properties.
        /// </summary>
        protected override void PostInit()
        {
            Threshold = 90;            
        }
    }
}
